import {getGroupListByDeptId,getGroupListByProjectId,getTypeList,getAllCompangList,getWorkTypeTotal } from "@/api/api.js"
export const groupCreate = {
    data() {
        return {
            groupkeyword:"",
            treeData:[],
            groupList:[],
            workTypeTotallist:[],
            groupEmpList: [],
            corpEmpList:[],
            corpList:[],
            workTypelist:[],
            workList:[],
        }
    },
    methods: {
        getGroupTreeList(id){
            this.treeData =[
                {
                    label:"全部",
                    id:0,
                    expand:true,
                    selected:true,
                    children:[],
                }
            ];
            let params ={
                deptId:id,
                keyword:this.groupkeyword,
            }
            getGroupListByDeptId(params).then(res=>{
                if(res.code == 200) {
                    if(res.data.list.length>0){
                        for (let i = 0 ; i<res.data.list.length;i++) {
                            this.treeData[0].children.push(
                                {
                                    label:res.data.list[i].corpName,
                                    id:"C"+res.data.list[i].corpId,
                                    expand:false,
                                    children:[],
                                }
                            )
                            if(res.data.list[i].groupList.length > 0 ) {
                                for(let j = 0 ; j< res.data.list[i].groupList.length; j++ ) {
                                    this.treeData[0].children[i].children.push(
                                        {
                                            label:res.data.list[i].groupList[j].groupName,
                                            id:"G"+res.data.list[i].groupList[j].groupId,
                                            expand:false,  
                                        }
                                    )
                                }
                            } else {
                                delete this.treeData[0].children[i].children;
                            }
                        }
                    }
                } else {
                    this.$Message.warning(res.msg);
                }
            }).catch(error =>{
                console.log(error)
            })
        },
        getGRPList(id){
            this.groupList = [];
            let params = {
                projectId :id,
            };
            getGroupListByProjectId(params)
                .then((res) => {
                    if (res.code == 200) {
                        this.groupEmpList = res.data.list
                        this.groupList.push({
                            groupId:"",
                            groupName: "班组:全部"
                        })
                        res.data.list.forEach(i=>{
                            this.groupList.push(i)
                        })
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        //获取工种
        getworkTypeTotalList(code) {
            let params = {
                code: code,
            };
            getTypeList(params)
                .then((res) => {
                    if (res.code == 200) {
                        this.workTypeTotallist = res.data.childList;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        //获取合作单位
        getcorpList(id) {
            this.corpList = [];
            let params = {
                deptId: id
            };
            getAllCompangList(params)
                .then(res => {
                    if (res.code == 200) {
                        this.corpEmpList = res.data.list;
                        this.corpList.push(
                            {
                                corpId:"",
                                corpName:"合作单位:全部"
                            }
                        )
                        this.corpEmpList.forEach(i=>{
                            this.corpList.push(i)
                        })
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        //获取工种
        getworkTypeList(id,boln) {
            this.workList = [];
            let params = {
                deptId:id
            };
            getWorkTypeTotal(params)
                .then((res) => {
                    if (res.code == 200) {
                        this.workTypelist = res.data.list;
                        this.workList.push({
                            workTypeId:"",
                            workTypeName:"工种:全部"
                        })
                        this.workTypelist.forEach(i=>{
                            this.workList.push(i)
                        })
                        if(!boln) {
                            this.crew.workTypeId = this.detailsObj.workTypeId;
                        }
                        
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
    },
    mounted() {
    },
}
